package com.aptemo.webetek.client.widgets.dialogs;

import com.aptemo.webetek.client.events.DecisionEvent;
import com.aptemo.webetek.client.events.DecisionHandler;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.TextBox;

public class CreateLabelDialog
		extends DialogBox implements HasDecisionHandlers, ClickHandler
{
	private TextBox	newLabelField	= new TextBox();
	private Button	createLabelBtn	= new Button("Create new label");

	public CreateLabelDialog(String title)
	{
		super();

		setText(title);

		FlexTable layout = new FlexTable();

		Button close = new Button("Close");
		close.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				hide();
			}
		});

		createLabelBtn.addClickHandler(this);

		layout.setWidget(0, 0, newLabelField);
		layout.getFlexCellFormatter().setColSpan(0, 0, 2);
		layout.setWidget(1, 1, createLabelBtn);
		layout.setWidget(1, 0, close);

		add(layout);
	}

	@Override
	public HandlerRegistration addDecisionHandler(DecisionHandler handler)
	{
		return addHandler(handler, DecisionEvent.TYPE);
	}

	public HasText getLabelField()
	{
		return newLabelField;
	}

	@Override
	public void onClick(ClickEvent event)
	{
		if (event.getSource() == createLabelBtn)
		{
			if (!newLabelField.getText().isEmpty())
			{

				fireEvent(new DecisionEvent(null));
				hide();
			}
		}
	}

}
